//
//  HMHomeViewModel.m
//  weibo
//
//  Created by apple on 2016/11/17.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "HMHomeViewModel.h"
#import <SDWebImageManager.h>
@implementation HMHomeViewModel
//https://api.weibo.com/2/statuses/home_timeline.json
+(instancetype)shared{
    static HMHomeViewModel *model;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        model = [[self alloc]init];
    });
    return model;
}

-(void)lodaHomeMessageispullup:(BOOL)ispullup finishedBlock:(void(^)(BOOL isFinished,NSInteger count))finishedBlock{
    
    NSString *urlString = @"https://api.weibo.com/2/statuses/home_timeline.json";
    NSInteger since = 0;
    NSInteger max = 0;
//    NSString *since = @"0";
//    NSString *max = @"0";
    if (ispullup) {
        max = self.hmstatusArr.lastObject.status.weiboID;
        if (max != 0) {
            max -= 1;
        }
    }else{
        since = self.hmstatusArr.firstObject.status.mid;
    }
    
    NSDictionary *parameters = @{@"access_token":[HMAccountViewModel shared].accountModel.access_token,
                                 @"max_id" : @(max),
                                 @"since_id" : @(since)
                                 };
    NSLog(@"%@",parameters);
[[HMSessionManger shared]requestMethod:GET urlString:urlString parameters:parameters finishedBlock:^(NSDictionary* finished, NSError *error) {
    if (error != nil) {
        [SVProgressHUD showErrorWithStatus:networkWarn];
        return ;
    }
    NSArray *arr = finished[@"statuses"];
    NSMutableArray<HMStatusViewModel *> *marr = [NSMutableArray array];
//    for (int i = 0; i < arr.count; i++) {
    for (NSDictionary *dic in arr) {
        HMStatusModel *status = [HMStatusModel yy_modelWithDictionary:dic];
//        for (int i = 0; i < status.pic_urls.count; i++) {
//            HMPictuerInfo *ss = status.pic_urls[i];
//            
//            NSLog(@"%@",ss.url_pic);
//
//        }
        HMStatusViewModel *statusV = [[HMStatusViewModel alloc]init];
        statusV.status = status;
        [marr addObject:statusV];
    }
    NSInteger num = marr.count;
    if (ispullup) {
      self.hmstatusArr =  [self.hmstatusArr arrayByAddingObjectsFromArray:marr];
    }else{
    [marr addObjectsFromArray:self.hmstatusArr];
      self.hmstatusArr = marr.copy;
    }
//    [self downLoadProvince:marr finishedBlock:finishedBlock];
    finishedBlock(YES,num);
}];

}
//
//-(void)downLoadProvince:(NSArray<HMStatusViewModel*>*)arr finishedBlock:(void(^)(BOOL isFinished,NSInteger count))finishedBlock{
//   dispatch_group_t group = dispatch_group_create();
////    NSLog(@"%@",arr);
//
//    for (int i = 0; i<arr.count; i++) {
//        if (arr[i].pictures.count == 1) {
//            dispatch_queue_t gq = dispatch_get_global_queue(0, 0);
//            dispatch_group_async(group, gq, ^{
//                HMPictuerInfo *info = arr[i].status.pic_urls.firstObject;
//                NSURL *url = [NSURL URLWithString:info.wap360];
//                NSLog(@"%@",url);
//                
//                [[SDWebImageManager sharedManager]downloadImageWithURL:url options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
//                    NSLog(@"completeblock");
//
//                }];
//            });
//            
//        }
//        
//    }
//    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//        NSLog(@"complete");
//            finishedBlock(YES,arr.count);
//
//    });
//}


@end
